$(function (){
    var queryString = window.location.search;
    var params = new URLSearchParams(queryString);
    username = params.get("username");
    page = params.get("page");
    $("#mainPage").click(function (){
        window.location='blog_list.html?username=' + username + '&page=1'
    })
    $("#editBlog").click(function (){
        window.location='blog_edit.html?username=' + username + '&page=' + page
    })
    $("#editBlog").click(function (){
        window.location='blog_edit.html?username=' + username + '&page=' + page
    })
    $("#square").click(function (){
        window.location='blog_square.html?username='+username+ '&page=' + page
    })
    $.ajax({
        url:"/blogSystem/square",
        data:{

        },
        dataType:"json",
        success:function (json){
            //console.log(JSON.stringify(json))
            console.log(JSON.stringify(json[0].author))
            console.log(JSON.stringify(json[0].comments))
            console.log(JSON.stringify(json[0].content))
            console.log(JSON.stringify(json[0].title))
            console.log(JSON.stringify(new Date(json[0].publishedTime).toLocaleDateString()))
            var jsonId=[]
            for (var i=0;i<json.length;i++){
                var html=`<div class="blog"> <div class="title">${json[i].title}</div>
                                <div class="date">${new Date(json[i].publishedTime).toLocaleDateString()}</div>
                                <div id="authorText">作者：${json[i].author}</div><div class="desc">${json[i].content.substring(0, 100)}...
                                </div>
                                <a href="javascript:void(0)" id="detailDisplay${json[i].id}" class="detail">查看详情&gt;&gt;</a></div>`
                $(".blogList").append(html)
                jsonId.push(json[i].id)
            }
            for (var i = 0; i < jsonId.length; i++) {
                $("body").on("click", "#detailDisplay" + jsonId[i] + "", (function(index) {
                    return function() {
                        console.log(jsonId[index])
                        window.location = "/blogSystem/blog/blog_specifics.html?articleId=" + jsonId[index]+"&username="+username
                    }
                })(i))
            }

        }
    })
})